package com.example.assisteddriving;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.assisteddriving.util.MyFragementpageAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    ViewPager2 viewPager2;
    private LinearLayout llzhuye,lltongji,llwode;
    private ImageView zhuyeim,tongjiim,wodeim,lsim;
    private TextView zhuyet,tongjit,wodet,lst;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initpager();
        initUI();
        requestPermission();
    }

    private void initUI() {
        llzhuye=findViewById(R.id.zhuyell);
        llzhuye.setOnClickListener(this);
        lltongji=findViewById(R.id.tongjill);
        lltongji.setOnClickListener(this);
        llwode=findViewById(R.id.wodell);
        llwode.setOnClickListener(this);

        zhuyeim=findViewById(R.id.zhuyeim);
        zhuyet=findViewById(R.id.zhuyet);

        tongjiim=findViewById(R.id.tongjiim);
        tongjit=findViewById(R.id.tongjit);

        wodeim=findViewById(R.id.wodeim);
        wodet=findViewById(R.id.wodet);

        zhuyeim.setSelected(true);
        zhuyet.setSelected(true);
        lsim=zhuyeim;
        lst=zhuyet;
    }

    private void initpager() {

        viewPager2=findViewById(R.id.viewpage1);
        viewPager2.setSaveEnabled(false);
        List<Fragment> fragments=new ArrayList<>();

        fragments.add(new Zhuye());
        fragments.add(new Tongji());
        fragments.add(new Wode());
        MyFragementpageAdapter pagerAdapter=new MyFragementpageAdapter(getSupportFragmentManager(),getLifecycle(),fragments);
        viewPager2.setAdapter(pagerAdapter);
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changetab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    private void changetab(int position) {
        lsim.setSelected(false);
        lst.setSelected(false);
        switch (position){
            case R.id.zhuyell:
                viewPager2.setCurrentItem(0);
            case 0:
                zhuyeim.setSelected(true);
                zhuyet.setSelected(true);
                lsim=zhuyeim;
                lst=zhuyet;
                break;
            case R.id.tongjill:
                viewPager2.setCurrentItem(1);
            case 1:
                tongjiim.setSelected(true);
                tongjit.setSelected(true);
                lsim=tongjiim;
                lst=tongjit;
                break;
            case R.id.wodell:
                viewPager2.setCurrentItem(2);
            case 2:
                wodeim.setSelected(true);
                wodet.setSelected(true);
                lsim=wodeim;
                lst=wodet;
                break;
        }

    }
    public void requestPermission() {
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS}, 1);
            Log.v("asd","申请短信权限");
        } else {
            Log.v("asd","短信已授权");
        }
    }
    @Override
    public void onClick(View view) {
        changetab(view.getId());
    }
}